﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common;

namespace AnesSystem.ViewModels
{
    public class CustomComboboxCtrlViewModel : ViewModel
    {
        #region Variables

        private List<CommonComboboxItemModel> _displayList = new List<CommonComboboxItemModel>();
        private List<CommonComboboxItemModel> _itemsSource = new List<CommonComboboxItemModel>();
        private string _text = "";
        private object thisLock = new object();
        #endregion

        #region Constructor

        public CustomComboboxCtrlViewModel()
        {
            IsFullDisplay = true;
        }

        #endregion

        #region Properties

        public List<CommonComboboxItemModel> DisplayList
        {
            get
            {
                return _displayList;
            }
            set
            {
                _displayList = value;
                RaisePropertyChanged("DisplayList");
            }
        }

        public bool IsFullDisplay { get; set; }

        public List<CommonComboboxItemModel> ItemsSource
        {
            get
            {
                return _itemsSource;
            }

            set
            {
                _itemsSource = value;
            }
        }

        private int _selectedIndex;
        public int SelectedIndex
        {
            get
            {
                return _selectedIndex;
            }

            set
            {
                _selectedIndex = value;
                RaisePropertyChanged("SelectedIndex");
            }
        }

        private CommonComboboxItemModel _selectedItem;
        public CommonComboboxItemModel SelectedItem
        {
            get
            {
                return _selectedItem;
            }
            set
            {
                _selectedItem = value;
                RaisePropertyChanged("SelectedItem");
            }
        }

        private bool _isDropDownOpen;

        public bool IsDropDownOpen
        {
            get
            {
                return _isDropDownOpen;
            }
            set
            {
                _isDropDownOpen = value;
                RaisePropertyChanged("IsDropDownOpen");
            }
        }

        public string Text
        {
            get
            {
                return _text;
            }

            set
            {
                _text = value;
                Search(value);
            }
        }



        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods

        #endregion

        #region Public/Protected Methods

        public void Search(string str)
        {
            try
            {

                Task.Factory.StartNew(delegate
                {
                    lock (thisLock)
                    {
                        if (ItemsSource != null)
                        {
                            if (IsFullDisplay)
                            {
                                DisplayList = ItemsSource.Where(O => !string.IsNullOrEmpty(O.Display) && O.Display.IsContain(str)).ToList();
                            }
                            else
                            {
                                DisplayList = ItemsSource.Where(O => !string.IsNullOrEmpty(O.Display) && O.Display.IsContain(str)).Take(10).ToList();
                            }
                            if (DisplayList.Count > 0)
                            {
                                SelectedItem = DisplayList[0];
                            }
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                Common.LogHelper.Error(this.GetType(), ex);
            }
        }

        public void DisplayAll()
        {
            DisplayList = ItemsSource;
        }

        public void UpOrDown(bool isUp)
        {
            if (!IsDropDownOpen)
                IsDropDownOpen = true;
            if (isUp && SelectedIndex > 0)
            {
                SelectedIndex--;
                SelectedItem = ItemsSource[SelectedIndex];
            }
            else if (!isUp && SelectedIndex < ItemsSource.Count)
            {
                SelectedIndex++;
                SelectedItem = ItemsSource[SelectedIndex];
            }
        }

        public string GetValues()
        {
            string values = "";
            List<CommonComboboxItemModel> list = ItemsSource.Where(O => O.IsSelected).ToList();
            if (list.Count == 1 && list.FirstOrDefault().Behind.Equals("-1"))
            {
                return list.FirstOrDefault().Display;
            }
            for (int i = 0; i < list.Count(); i++)
            {
                CommonComboboxItemModel model = list[i];
                if (i == list.Count() - 1)
                {
                    values += model.Behind;
                }
                else
                {
                    values += model.Behind + ";";
                }
            }
            return values;
        }

        public void CancelOtherChecked(CommonComboboxItemModel model)
        {
            if (model.Behind.Equals("-1"))
            {
                IEnumerable<CommonComboboxItemModel> list = ItemsSource.Where(O => !O.Behind.Equals(model.Behind) && O.IsSelected);
                foreach (var item in list)
                {
                    item.IsSelected = false;
                }
            }
            else
            {
                IEnumerable<CommonComboboxItemModel> list = ItemsSource.Where(O => O.Behind.Equals("-1") && O.IsSelected);
                foreach (var item in list)
                {
                    item.IsSelected = false;
                }
            }
        }

        #endregion

        #region EventHandlers

        #endregion
    }
}
